package com.jay.tools.utils.file_util;

import android.os.Environment;
import android.text.format.DateFormat;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;

/**
 * description: 将日志输出到本地sd卡
 * author: tjp
 * date: 2017/7/12 012 16:34
 */
public class LogSDUtil {

    private static final String sPATH = Environment.getExternalStorageDirectory() + "/mylogs/";

    private LogSDUtil() {
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    public static void saveTest(Class clazz, String msg) {
        saveInfo(clazz, msg, "test-");
    }

    public static void saveLog(Class clazz, String msg) {
        saveInfo(clazz, msg, "log-");
    }

    public static void saveUpload(Class clazz, String msg) {
        saveInfo(clazz, msg, "uploadfile-");
    }

    public static void saveDrops(Class clazz, String msg) {
        saveInfo(clazz, msg, "drops-");
    }

    public static void saveFtp(Class clazz, String msg) {
        saveInfo(clazz, msg, "ftp-");
    }

    private static void saveInfo(Class clazz, String msg, String fileType) {
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
            return;
        try {
            File directory = new File(sPATH);
            if (!directory.exists()) {
                directory.mkdirs();
            }
            String time1 = (String) DateFormat.format("yyyy-MM-dd", new Date());
            String fileName = fileType + time1 + ".txt";
            FileOutputStream fos = new FileOutputStream(sPATH + fileName, true);

            String time = (String) DateFormat.format("yyyy-MM-dd HH:mm:ss", new Date());
            fos.write(time.getBytes());
            if (clazz != null) {
                fos.write(("--" + clazz.getSimpleName()).getBytes());
            }
            fos.write(("\n" + msg + "\n\n").getBytes());
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
